﻿using System;
using System.Collections.Generic;
using System.Windows;

#if WPF
using System.Windows.Data;
using System.Windows.Shapes;
#else
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Data;
#endif
using System.Linq;

namespace Eternal.DataVisualization
{
    public abstract class StockType : AxisType
    {

        public string HighValuePath
        {
            get { return (string)GetValue(HighValuePathProperty); }
            set { SetValue(HighValuePathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HighValuePath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HighValuePathProperty =
            DependencyProperty.Register("HighValuePath", typeof(string), typeof(StockType), new PropertyMetadata(string.Empty));



        public string LowValuePath
        {
            get { return (string)GetValue(LowValuePathProperty); }
            set { SetValue(LowValuePathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LowValuePath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LowValuePathProperty =
            DependencyProperty.Register("LowValuePath", typeof(string), typeof(StockType), new PropertyMetadata(string.Empty));


        protected IList<double> ActualHigh;
        protected IList<double> ActualLow;

        internal override void UpdateRange()
        {
            if (PointsGenerated)
            {
                MinimumX = 0;
                MaximumX = ActualXData.Count - 1;
                DeltaX = MaximumX - MinimumX;
                MaximumY = Convert.ToDouble(ActualHigh.Max());
                MinimumY = Convert.ToDouble(ActualLow.Min());
                DeltaY = MaximumY - MinimumY;
                DeltaY = DeltaY == 0 ? 1 : DeltaY;
                DeltaX = DeltaX == 0 ? 1 : DeltaX;

                if (XAxis != null && YAxis != null)
                {
                    YAxis.ActualEnd = Math.Max(YAxis.ActualEnd, MaximumY);
                    YAxis.ActualStart = Math.Min(YAxis.ActualStart, MinimumY);
                    XAxis.ActualEnd = Math.Max(XAxis.ActualEnd, MaximumX);
                    XAxis.ActualStart = Math.Min(XAxis.ActualStart, MinimumX);
                }

                IsRangeUpdateNeeded = false;
            }
        }

        internal override void GenerateDatas()
        {
            if (ItemsSource != null && !String.IsNullOrEmpty(PrimaryPath))
            {
                var type = "";
                ActualXData = DataIterator.Iterate(ItemsSource, PrimaryPath, ref type);
                XDataType = type;
                ActualHigh = DataIterator.IterateDoubleValue(ItemsSource, HighValuePath, ref type);
                ActualLow = DataIterator.IterateDoubleValue(ItemsSource, LowValuePath, ref type);
                YDataType = type;
                PointsGenerated = true;
                IsRangeUpdateNeeded = true;
            }
        }
    }
}
